Raspberry Pi: 2 Beginner's Users Manual (Tech Geek) by Alex Benjamin

Raspberry Pi: 2 Beginner's Users Manual (Tech Geek) by Alex Benjamin

Author:Alex Benjamin [Benjamin, Alex]
Language: eng
Format: epub
Published: 2015-06-04T18:30:00+00:00


Click the Run button. If you have hooked up your Raspberry Pi via an HDMI cable and typed in the command correctly, you should be able to hear a fascinating sequence of bells, called Haunted Bells. The code you just typed in is what is producing this sounds. Let’s dissect the code.

The keywords loop do … end form a repetitive loop that will run until you stop the program.

The command sample is used to play a recorded sound file. In this case, it is playing the sound of a perc_bell, or percussive bell.

The rate is how fast the sound plays and at what frequency. In this case, each time the bell is played its speed varies for a random amount of time, between 0.5 to 1.5 seconds. If you set the rate to 0.5, it plays at ½ speed and at an octave down in pitch, or ½ the original frequency. If you set the rate to 2.0, it plays at double speed and twice the original frequency. For those of us not so musically inclined, the higher the number, the higher the bell sounds. Very low numbers cause it to have a deep sound.

The sleep command controls how long before the next command is triggered in beats per minute. In this example, it will wait a random amount of time between 0 and 2 beats before executing the next command.

Press the Start button to hear it. Since this is setup as a loop, it will keep playing random bell sounds until we use the Stop button. It sounds somewhat like a wind chime, doesn’t it?

One of the best ways to learn programming is by experimenting, so before you go on try changing the rate and the sleep to see what happens. Stop the music before you run the next sound. For example, if you set the rate to be a random number between 1.0 and 1.5 there is not much difference in the sound of the bells, and they sound relatively high pitched.

Let’s add a bit more interest to our wind chime effect. Try adding a pan parameter to adjust the position of the sound in stereo: the default is 0, or equally sounding in both ears; a -1 moves the sound to the left ear, and a + 1 moves the sound to the right ear.

loop do

sample :perc_bell, rate: (rrand 0.125, 1.5), pan: (rrand -1,1)

sleep rrand(0, 2)

end



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.